package com.redmoon.dingding.service.role;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.cache.RoleCache;
import com.cloudweb.oa.cache.UserCache;
import com.cloudweb.oa.entity.Role;
import com.cloudweb.oa.service.IRoleService;
import com.cloudweb.oa.utils.ConstUtil;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRoleListRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiRoleListResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.redmoon.dingding.service.BaseService;
import com.redmoon.dingding.service.auth.AuthService;
import com.redmoon.oa.basic.SelectDb;
import com.redmoon.oa.basic.SelectMgr;
import com.redmoon.oa.basic.SelectOptionDb;
import com.taobao.api.ApiException;

import java.util.Vector;

public class RoleService extends BaseService {

    public void syncRoles() {
        SelectDb sd = new SelectDb();
        sd = sd.getSelectDb("role_kind");
        SelectMgr sm = new SelectMgr();
        Vector<SelectOptionDb> opts = sd.getOptions();
        int orders = sd.getOptions().size() + 1;
        RoleCache roleCache = SpringUtil.getBean(RoleCache.class);
        IRoleService roleService = SpringUtil.getBean(IRoleService.class);

        String accessToken = AuthService.getAccessToken();
        try {
            boolean hasMore = true;
            DingTalkClient client = new DefaultDingTalkClient(URL_ROLE_LIST);
            OapiRoleListRequest req = new OapiRoleListRequest();
            while (hasMore) {
                req.setSize(200L);
                req.setOffset(0L);
                OapiRoleListResponse rsp = client.execute(req, accessToken);
                JSONObject json = JSONObject.parseObject(rsp.getBody());
                JSONObject result = json.getJSONObject("result");
                hasMore = result.getBoolean("hasMore");
                JSONArray arr = result.getJSONArray("list");
                for (Object o : arr) {
                    JSONObject jsonObject = (JSONObject)o;
                    long groupId = jsonObject.getLongValue("groupId");
                    String name = jsonObject.getString("name");

                    // 判断是否已存在组
                    boolean isFound = false;
                    for (SelectOptionDb sod : opts) {
                        if (sod.getValue().equals(String.valueOf(groupId))) {
                            isFound = true;
                            break;
                        }
                    }
                    // 如不存在则创建组
                    if (!isFound) {
                        SelectOptionDb sod = new SelectOptionDb();
                        sod.setValue(String.valueOf(groupId));
                        sod.setName(name);
                        sod.setCode("role_kind");
                        sm.createOption(sd.getCode(), name, String.valueOf(groupId),false, orders, "", true);
                        orders++;
                    }

                    // 如不存在则创建角色
                    JSONArray roles = jsonObject.getJSONArray("roles");
                    for (Object obj : roles) {
                        JSONObject jsonRole = (JSONObject)obj;
                        long roleId = jsonRole.getLongValue("id");
                        String roleName = jsonRole.getString("name");
                        Role role = roleCache.getRole(String.valueOf(roleId));
                        if (role == null) {
                            roleService.create(String.valueOf(roleId), roleName, 0, 0, -1, ConstUtil.DEPT_ROOT, -1, 0, "", null);
                        }
                    }
                }
            }
        } catch (ApiException e) {
            LogUtil.getLog(getClass()).error(e);
        }
    }
}
